<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="realname">
        <el-input v-model="queryParams.realname" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 320px" type="datetimerange" align="right"
          start-placeholder="开始时间" end-placeholder="结束时间" :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['userManager:user:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="id" />
      <!-- <el-table-column label="名" align="center" prop="userName" /> -->
      <el-table-column label="姓名" align="center" prop="realname" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="账户余额" align="center" prop="academicCurrency" />
      <el-table-column label="科藤备注" align="center" prop="remark" />

      <el-table-column label="更新时间" align="center" prop="updateTime" />


      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="lookDetail(scope.row)"
            v-hasPermi="['finance:deposit:detail']">查看详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="updateOperation(scope.row, 'visiable')"
            v-hasPermi="['finance:change:money']">变更余额</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <chageRecharge title="变更余额" :visiable="visiable" :item="row" @close="close"></chageRecharge>
  </div>
</template>
<script>
import { listUser, getUser, addUser, updateUser } from "@/api/user";
import { ARRERS, USER_SEARCH_PARAM } from "../../../constant/const";
import { timestampToDate } from "../../../utils/time"

import { updateOperation, close } from "../../../jscomponent/recharge"

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户管理表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //  变更余额 弹框
      visiable: false,

      row: {},

      recommendVisiable: false,
      // 当前点击行的id
      clickId: 0,

      duplicateURL: "/userManager/user/duplicateCheckCoupon",

      updateRecommendURL: "/userManager/user/updateRecommend",
      dateRange: [],
      arrears: ARRERS,
      // 查询参数
      queryParams: USER_SEARCH_PARAM,
      isSearch: false
    };
  },
  created() { },
  mounted() {
    this.getList();
  },

  components: {
    chageRecharge: () => import("./components/changeRecharge.vue")
  },

  methods: {
    close,

    lookDetail(row) {

      localStorage.setItem("depositItem", JSON.stringify(row));


      this.$router.push({
        path: `/finance/deposit/edit/${row.id}`
      });
    },

    /** 查询用户管理列表 */
    getList() {
      this.loading = true;
      let params = {}
      console.log(this.isSearch, "56565");
      if (this.isSearch) {
        params = Object.assign({}, this.queryParams);
      
        params.startTime = (this.dateRange && this.dateRange.length) ? new Date(this.dateRange[0]).getTime() / 1000 : null
        params.endTime = (this.dateRange && this.dateRange.length) ? new Date(this.dateRange[1]).getTime() / 1000 : null
      } else {
        params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
        }
      }
      listUser(params).then((response) => {
        this.userList = response.rows;

        this.userList.forEach(element => {
          element.updateTime = timestampToDate(element.updateTime);
        })

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        wxCode: null,
        password: null,
        salt: null,
        avatar: null,
        realname: null,
        mobile: null,
        email: null,
        sex: null,
        description: null,
        organizationCode: null,
        age: null,
        teamId: null,
        birthday: null,
        identity: null,
        status: null,
        lastLoginTime: null,
        recommendedUserId: null,
        lastLoginIp: null,
        createTime: null,
        updateTime: null,
        
      };
      this.resetForm("form");
    },

    updateOperation,

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.isSearch = true;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");

      this.dateRange = undefined;
      // this.queryParams = {...RECHARGE_PARAM};
      this.isSearch = false;
      this.queryParams.pageNum = 1;
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },



    closeConpon(visiable) {
      this.duplicateVisiable = visiable;
    },

    closeRecommend(visiable) {
      this.recommendVisiable = visiable;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "userManager/user/exportByDepoist",
        {
          ...this.queryParams,
          ids: this.ids.length? this.ids : undefined
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>